package com.hzxy.yiyid.web.common.base;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.beans.Transient;
import java.io.Serializable;

/**
 * @ClassName BaseResult
 * @Description TOOD
 * @Author RAICOM2023
 * @Date 2023/3/29 8:16
 * @Version 1.0
 **/
@Data
public class BaseResult<T> implements Serializable {
    private static final long serialVersionUID = 1268219481959950151L;
    public static final int SUCCESS_TYPE = 0;
    public static final int ERROR_TYPE = -1;
    public static final int FAIL_TYPE = -2;
    public static final int NO_AUTH_TYPE = -3;

    @ApiModelProperty(
            required = true,
            value = "结果类型：0成功;-1错误，系统未处理;-2异常，系统已处理",
            dataType = "int",
            example = "0",
            position = 0
    )
    private int type = -1;
    @ApiModelProperty(
            required = true,
            value = "错误码",
            dataType = "String",
            example = "0",
            position = 1
    )
    private String code;
    @ApiModelProperty(
            required = true,
            value = "返回描述信息",
            dataType = "String",
            example = "SUCCESS",
            position = 2
    )
    private String msg;
    @ApiModelProperty(
            value = "返回数据封装",
            position = 3
    )
    private T data;

    public BaseResult() {
    }

    public BaseResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BaseResult(int type, String code, String msg) {
        this.type = type;
        this.code = code;
        this.msg = msg;
    }

    public BaseResult(int type, String code, String msg, T data) {
        this.type = type;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> BaseResult<T> success(T t) {
        return new BaseResult(0, "200", "SUCCESS", t);
    }

    public static <T> BaseResult<T> fail(String code, String message) {
        return new BaseResult(-2, code, message);
    }

    public static <T> BaseResult<T> error(String code, String message) {
        return new BaseResult(-1, code, message);
    }

    @Transient
    public boolean isSuccess() {
        return this.type == 0;
    }

    @Transient
    public boolean isFail() {
        return this.type == -2;
    }

    @Transient
    public boolean isError() {
        return this.type == -1;
    }
}
